package com.atguigu.springcloud.alibaba.service.impl;

import com.atguigu.springcloud.alibaba.dao.OrderDao;
import com.atguigu.springcloud.alibaba.domain.Order;
import com.atguigu.springcloud.alibaba.service.AccountService;
import com.atguigu.springcloud.alibaba.service.OrderService;
import com.atguigu.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.pl.REGON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;

    /**
     * 下订单-->改库存-->改状态-->完成
     * @param order
     */
    @Override
    @GlobalTransactional(name = "zjp-create-order", rollbackFor = Exception.class)
    public void create(Order order) {
        //1.创建订单
        log.info("----------->开始新建订单");
        orderDao.create(order);

        //2.库存减少
        log.info("------------>订单微服务开始调用库存，做扣减Count");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("------------>订单微服务调用库存扣减结束");

        //3.账户余额减少
        log.info("------------>订单微服务开始调用账户，做扣减Money");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("------------>订单微服务调用账户扣减结束");

        //4.修改订单状态0->1，代表已经完成
        log.info("----------->开始修改订单状态");
        orderDao.update(order.getUserId(), 0);
        log.info("----------->修改订单状态结束");

        log.info("----------下订单完成--------------");
    }
}
